package com.ruoyi.fts.controller;

import java.math.BigDecimal;
import java.util.Date;
import java.util.List;
import javax.servlet.http.HttpServletResponse;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.ruoyi.fts.domain.FtsOrder;
import com.ruoyi.fts.service.IFtsOrderService;
import com.ruoyi.fts.wxconfig.WxJsapiService;
import com.wechat.pay.java.service.refund.model.AmountReq;
import com.wechat.pay.java.service.refund.model.CreateRequest;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.fts.domain.FtsRefundOrder;
import com.ruoyi.fts.service.IFtsRefundOrderService;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 退款订单Controller
 * 
 * @author lumberer
 * @date 2024-11-09
 */
@RestController
@RequestMapping("/fts/refund")
public class FtsRefundOrderController extends BaseController
{
    @Autowired
    private IFtsRefundOrderService ftsRefundOrderService;
    @Autowired
    private IFtsOrderService ftsOrderService;
    @PreAuthorize("@ss.hasPermi('fts:order:list')")
    @GetMapping("/refund")
    public AjaxResult refund(HttpServletResponse response, FtsRefundOrder ftsRefundOrder){
        QueryWrapper queryWrapper=new QueryWrapper();
        queryWrapper.eq("order_code",ftsRefundOrder.getOrderCode());
        FtsOrder ftsOrder=ftsOrderService.getOne(queryWrapper);

        String outRefundNo=WxJsapiService.outTradeNoGen();
        ftsRefundOrder.setRefundOrderCode(outRefundNo);
        ftsRefundOrder.setUserId(ftsOrder.getPayUserId());
        ftsRefundOrder.setPrice(ftsOrder.getPrice());
        ftsRefundOrder.setDisposeTime(new Date());
        ftsRefundOrder.setDisposeUser(getUsername());
        ftsRefundOrder.setUpdateUserId(getUserId());
        ftsRefundOrder.setUpdateTime(new Date());
        ftsRefundOrder.setUpdateBy(getUsername());

        if(ftsRefundOrder.getRefundState()==3){
            //同意退款，走微信逻辑
            CreateRequest request=new CreateRequest();
            request.setOutTradeNo(ftsRefundOrder.getOrderCode());

            request.setOutRefundNo(outRefundNo);
            request.setReason("用户退款");
            AmountReq amount=new AmountReq();
            BigDecimal price=ftsOrder.getPrice().multiply(new BigDecimal(100));
            amount.setRefund(price.longValue());
            amount.setTotal(price.longValue());
            amount.setCurrency("CNY");
            request.setAmount(amount);
            WxJsapiService.refund(request);

            ftsRefundOrderService.save(ftsRefundOrder);
            ftsOrder.setRefundState(ftsRefundOrder.getRefundState());
            ftsOrder.setState(4);
            ftsOrder.setUpdateUserId(getUserId());
            ftsOrder.setUpdateBy(getUsername());
            ftsOrder.setUpdateTime(new Date());
            return toAjax(ftsOrderService.updateById(ftsOrder));
        }else{
            ftsRefundOrderService.save(ftsRefundOrder);
            ftsOrder.setRefundState(ftsRefundOrder.getRefundState());
            ftsOrder.setState(4);
            //其他，走内部逻辑
            return toAjax(ftsRefundOrderService.save(ftsRefundOrder));
        }
    }
    /**
     * 查询退款订单列表
     */
    @PreAuthorize("@ss.hasPermi('fts:refund:list')")
    @GetMapping("/list")
    public TableDataInfo list(FtsRefundOrder ftsRefundOrder)
    {
        startPage();
        List<FtsRefundOrder> list = ftsRefundOrderService.selectFtsRefundOrderList(ftsRefundOrder);
        return getDataTable(list);
    }

    /**
     * 导出退款订单列表
     */
    @PreAuthorize("@ss.hasPermi('fts:refund:export')")
    @Log(title = "退款订单", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, FtsRefundOrder ftsRefundOrder)
    {
        List<FtsRefundOrder> list = ftsRefundOrderService.selectFtsRefundOrderList(ftsRefundOrder);
        ExcelUtil<FtsRefundOrder> util = new ExcelUtil<FtsRefundOrder>(FtsRefundOrder.class);
        util.exportExcel(response, list, "退款订单数据");
    }

    /**
     * 获取退款订单详细信息
     */
    @PreAuthorize("@ss.hasPermi('fts:refund:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id)
    {
        return success(ftsRefundOrderService.selectFtsRefundOrderById(id));
    }

    /**
     * 新增退款订单
     */
    @PreAuthorize("@ss.hasPermi('fts:refund:add')")
    @Log(title = "退款订单", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody FtsRefundOrder ftsRefundOrder)
    {
        ftsRefundOrder.setCreateUserId(getUserId());
        ftsRefundOrder.setCreateBy(getUsername());
        return toAjax(ftsRefundOrderService.insertFtsRefundOrder(ftsRefundOrder));
    }

    /**
     * 修改退款订单
     */
    @PreAuthorize("@ss.hasPermi('fts:refund:edit')")
    @Log(title = "退款订单", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody FtsRefundOrder ftsRefundOrder)
    {
        ftsRefundOrder.setUpdateUserId(getUserId());
        ftsRefundOrder.setUpdateBy(getUsername());
        return toAjax(ftsRefundOrderService.updateFtsRefundOrder(ftsRefundOrder));
    }

    /**
     * 删除退款订单
     */
    @PreAuthorize("@ss.hasPermi('fts:refund:remove')")
    @Log(title = "退款订单", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids)
    {
        return toAjax(ftsRefundOrderService.deleteFtsRefundOrderByIds(ids));
    }
}
